package com.tianma315.admin.statisticstable.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.exception.HtmlCodeException;
import com.tianma315.admin.common.exception.IFastException;
import com.tianma315.admin.common.service.ConfigService;
import com.tianma315.admin.common.type.EnumErrorCode;
import com.tianma315.admin.statisticstable.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private ConfigService configService;

    @GetMapping("/choice")
    public String statisticsProduct(){
        return "statistics_table/choice";
    }

    @GetMapping("/checkPassword")
    @ResponseBody
    public boolean checkPassword(String password){
        String value = configService.getValuByKey("check_password");
        if (!password.equals(value)){
          return false;
        }
        return true;
    }

    @GetMapping("/productPage")
    @ResponseBody
    public Page productPage(Integer pageNumber, Integer pageSize,HttpSession session){
        List<Integer> ids = (List)session.getAttribute("ids");
        Date beginDate = (Date)session.getAttribute("beginDate");
        Date endDate =(Date) session.getAttribute("endDate");
        Page page = statisticsService.statisticsProductTable(pageNumber,pageSize,ids,beginDate,endDate);
        return page;
    }


    @GetMapping("/materialPage")
    @ResponseBody
    public Page materialList(Integer pageNumber, Integer pageSize,HttpSession session){
        List<Integer> ids = (List)session.getAttribute("ids");
        Date beginDate = (Date)session.getAttribute("beginDate");
        Date endDate =(Date) session.getAttribute("endDate");
        Page page = statisticsService.statisticsMaterialTable(pageNumber,pageSize,ids,beginDate,endDate);
       return page;
    }

    @PostMapping("/statisticsTable")
    public String statisticsProductTable(@RequestParam(value = "ids",required = false) List<Integer> ids, @RequestParam("beginDate") Date beginDate,
                                         @RequestParam("endDate") Date endDate, @RequestParam("checkPassword")String password,
                                         @RequestParam("type")Integer type,
                                         Model model, HttpSession session){
        session.setAttribute("ids",ids);
        session.setAttribute("beginDate",beginDate);
        session.setAttribute("endDate",endDate);
        String value = configService.getValuByKey("check_password");
        if (!password.equals(value)){
            throw new HtmlCodeException("检测密码错误");
        }

        //根据type区分是统计产品检测数据还是原材料 0：产品， 1：原材料
        if (type.equals(0)){
            if (ids!=null&&ids.size()!=0){
                Page page = statisticsService.statisticsProductTable(1,10,ids,beginDate,endDate);
                model.addAttribute("page",page);
                return "statistics_table/productTable";
            }else{

                JSONObject jsonObject = statisticsService.statisticsProductAllTable(beginDate,endDate);
                model.addAttribute("result",jsonObject);
                return "statistics_table/productAllTable";
            }

        }else {

            if (ids!=null&&ids.size()!=0){
                Page page = statisticsService.statisticsMaterialTable(1,10,ids,beginDate,endDate);
                model.addAttribute("page",page);
                return "statistics_table/materialTable";
            }else{
                JSONObject jsonObject = statisticsService.statisticsMaterialAllTable(ids,beginDate,endDate);
                model.addAttribute("result",jsonObject);
                return "statistics_table/materialAllTable";
            }

        }
    }

}
